package com.atguigu.spzx.test;

public class ThreadLocalExample {
    // 创建一个ThreadLocal对象，存储Integer类型的变量
    private static ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 1);

    public static void main(String[] args) {
        Runnable task1 = () -> {
            System.out.println("Thread 1 initial value: " + threadLocal.get());
            threadLocal.set(100);
            System.out.println("Thread 1 new value: " + threadLocal.get());
        };

        Runnable task2 = () -> {
            System.out.println("Thread 2 initial value: " + threadLocal.get());
            threadLocal.set(200);
            System.out.println("Thread 2 new value: " + threadLocal.get());
        };

        Thread thread1 = new Thread(task1);
        Thread thread2 = new Thread(task2);

        thread1.start();
        thread2.start();
    }
}
